/**
 * 
 */
package tree;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;

/**
 * @author Ken
 *
 */
public class IconCellRenderer extends JLabel implements TreeCellRenderer{
	public static ImageIcon ICON_DATABASE;
    public static ImageIcon ICON_TABLE;
    
    protected Color m_textSelectionColor;
    protected Color m_textNonSelectionColor;
    protected Color m_bkSelectionColor;
    protected Color m_bkNonSelectionColor;
    protected Color m_borderSelectionColor;
    protected boolean m_selected;	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public IconCellRenderer() {
		super();
        ICON_DATABASE = new ImageIcon(getClass().getResource("/res/database-icon-16.png"));
        ICON_TABLE = new ImageIcon(getClass().getResource("/res/table_tree-16.png"));                
        m_textSelectionColor = UIManager.getColor(
            "Tree.selectionForeground");
        m_textNonSelectionColor = UIManager.getColor(
            "Tree.textForeground");
        m_bkSelectionColor = UIManager.getColor(
            "Tree.selectionBackground");
        m_bkNonSelectionColor = UIManager.getColor(
            "Tree.textBackground");
        m_borderSelectionColor = UIManager.getColor(
            "Tree.selectionBorderColor");
        setOpaque(false);
	}
	
	
	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean selected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
        Object obj = node.getUserObject();
        setText(obj.toString());
        if (obj instanceof Boolean)
          setText("Retrieving data...");
        if (obj instanceof IconData) {
          IconData idata = (IconData)obj;          
          setIcon(idata.getIcon());
        }
        else
          setIcon(null);
        setFont(tree.getFont());
        setForeground(selected ? m_textSelectionColor : m_textNonSelectionColor);
        setBackground(selected ? m_bkSelectionColor : m_bkNonSelectionColor);
        m_selected = selected;
        return this;
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		Color bColor = getBackground();
        Icon icon = getIcon();
        g.setColor(bColor);
        int offset = 0;
        if(icon != null && getText() != null) 
          offset = (icon.getIconWidth() + getIconTextGap());
        g.fillRect(offset, 0, getWidth() - 1 - offset,
          getHeight() - 1);
        if (m_selected) {
            g.setColor(m_borderSelectionColor);
            g.drawRect(offset, 0, getWidth()-1-offset, getHeight()-1);
        }
        super.paintComponent(g);              
	}
}